/*
 * @Company: TWL
 * @Author: xue jian
 * @Email: xuejian@kanzhun.com
 * @Date: 2019-12-30 15:42:43
 */
#include "nlohmann/json.hpp"
using json = nlohmann::json;
#include "rocksdb/db.h"
#include <rocksdb/metadata.h>


json get_json_from_livemeta(const rocksdb::LiveFileMetaData& meta) {
    json re;
    re["column_family_name"] = meta.column_family_name;  // Name of the column family
    re["level"] = meta.level;                       // Level at which this file resides.
    re["size"] = meta.size;
    // The name of the file.
    re["name"] = meta.name;
    // The full path where the file locates.
    re["db_path"] = meta.db_path;

    re["smallest_seqno"] = meta.smallest_seqno;  // Smallest sequence number in file.
    re["smallest_seqno"] = meta.largest_seqno;   // Largest sequence number in file.
    re["smallestkey"] = meta.smallestkey;        // Smallest user defined key in the file.
    re["largestkey"] = meta.largestkey;         // Largest user defined key in the file.
    re["num_reads_sampled"] = meta.num_reads_sampled;     // How many times the file is read.
    re["being_compacted"] = meta.being_compacted;  // true if the file is currently being compacted.

    re["num_entries"] = meta.num_entries;
    re["num_deletions"] = meta.num_deletions;


    
    return re;
}


rocksdb::LiveFileMetaData get_livemeta_from_json(const json& j_meta) {
    rocksdb::LiveFileMetaData meta;
    meta.column_family_name = j_meta["column_family_name"] ;  // Name of the column family
    meta.level = j_meta["level"];                       // Level at which this file resides.
    meta.size = j_meta["size"];
    // The name of the file.
    meta.name = j_meta["name"];
    // The full path where the file locates.
    meta.db_path = j_meta["db_path"];

    meta.smallest_seqno = j_meta["smallest_seqno"];  // Smallest sequence number in file.
    meta.largest_seqno = j_meta["smallest_seqno"];   // Largest sequence number in file.
    meta.smallestkey = j_meta["smallestkey"];        // Smallest user defined key in the file.
    meta.largestkey = j_meta["largestkey"];         // Largest user defined key in the file.
    meta.num_reads_sampled = j_meta["num_reads_sampled"];     // How many times the file is read.
    meta.being_compacted = j_meta["being_compacted"];  // true if the file is currently being compacted.

    meta.num_entries = j_meta["num_entries"];
    meta.num_deletions = j_meta["num_deletions"];

    return meta;
}


json get_json_from_sstmeta(const rocksdb::ExportImportFilesMetaData& meta) {
    json re;
    re["db_comparator_name"] = meta.db_comparator_name;
    for (auto& file:meta.files) {
        re["files"].push_back(get_json_from_livemeta(file));
    }
    return re;
}


rocksdb::ExportImportFilesMetaData get_sstmeta_from_json(const json& j_meta) {
    rocksdb::ExportImportFilesMetaData meta;
    meta.db_comparator_name = j_meta["db_comparator_name"];
    for (auto& file:j_meta["files"]) {
        meta.files.push_back(get_livemeta_from_json(file));
    }
    return meta;
}